package dao;

import hbt.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class GenericDAO {
	private static GenericDAO instancia = null;
	private static SessionFactory sf = null;
	private Session session = null;
	
	public static GenericDAO getInstancia()
	{
		if(instancia == null)
		{
			sf = HibernateUtil.getSessionFactory();
			instancia = new GenericDAO();
		} 
		return instancia;
	}
	
	public Session getSession()
	{
		if(session == null || !session.isOpen())
			session = sf.openSession();
		return session;
	}
	
	public void closeSession()
	{
		if (session.isOpen()) session.close();
	}
	
	public void persist(Object obj)
	{
		Session s=getSession();
		s.beginTransaction();
		s.persist(obj);
		s.flush();
		s.getTransaction().commit();
		closeSession();
	}
	
	public void update(Object obj)
	{
		Session s= getSession();
		s.beginTransaction();
		s.update(obj);
		s.flush();
		s.getTransaction().commit();
		closeSession();
	}
}
